import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:flutter_my_picker/common/date.dart';
import 'package:path_provider/path_provider.dart';

class UpdateDialog extends StatefulWidget {
  final key;
  final UpgradeInfo upgradeInfo;
  final Function onClickWhenDownload;
  final Function onClickWhenNotDownload;

  UpdateDialog({
    this.key,
    this.upgradeInfo,
    this.onClickWhenDownload,
    this.onClickWhenNotDownload,
  });

  @override
  State<StatefulWidget> createState() => new UpdateDialogState();
}

class UpdateDialogState extends State<UpdateDialog> {
  int _downloadProgress = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getExternalStorageDirectory().then((tempDir){
      String tempPath = tempDir.path;
      String savePath = '$tempPath/update_${widget.upgradeInfo.versionName}.apk';
      File file=File(savePath);
      file.exists().then((value) async {
        setState(() {
          if(value){
            _downloadProgress=100;
          }else{
            _downloadProgress=0;
          }
        });
      });
    });

  }
  @override
  Widget build(BuildContext context) {
    var _textStyle =
    new TextStyle(fontSize: 12);

    return new AlertDialog(
      backgroundColor: Colors.transparent,
      contentPadding: EdgeInsets.zero,
      content: buildContent(_textStyle)
//      actions: <Widget>[
//        new FlatButton(
//          child: new Text(
//            '更新',
//            style: _textStyle,
//          ),
//          onPressed: () {
//            if (_downloadProgress != 0.0) {
//              widget.onClickWhenDownload("正在更新中");
//              return;
//            }
//            widget.onClickWhenNotDownload();
////            Navigator.of(context).pop();
//          },
//        ),
//        new FlatButton(
//          child: new Text('取消'),
//          onPressed: () {
//            Navigator.of(context).pop();
//          },
//        ),
//      ],
    );
  }

  Widget buildContent(TextStyle _textStyle) {
    double width=MediaQuery.of(context).size.width-48;
    double height=MediaQuery.of(context).size.height/2;
    var btnTxt='立即更新';
    if(_downloadProgress==0){
      btnTxt='立即更新';
    }else if(_downloadProgress>0&&_downloadProgress<100){
      btnTxt='$_downloadProgress%';
    }else{
      btnTxt='安装';
    }
    return Container(
      height: height,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Stack(
            alignment: Alignment.bottomCenter,
            children: [
              Image.asset(
                'assets/tc_head.png',
//            width: double.infinity,
                height: width*382/792,
                fit: BoxFit.fill,
              ),
              Positioned(
                bottom: 15,
                  child: Text(
                '版本：v${widget.upgradeInfo.versionName}    大小：${getPrintSize(widget.upgradeInfo.fileSize)}\n更新时间：${MyDate.format('yyyy-MM-dd HH:mm:ss', DateTime.fromMillisecondsSinceEpoch(widget.upgradeInfo.publishTime))}',
                    style: TextStyle(color: Colors.white,fontSize: 10),
              ))
            ],
          ),
          Expanded(
            child: Container(
              color: Colors.white,
              child: SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.all(10),
                  color: Colors.white,
                  child: Text(
                    "更新说明：\n${widget.upgradeInfo.newFeature}",
                    style: _textStyle,
                  ),
                ),
              ),
            ),
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.vertical(bottom:Radius.circular(5))),
            child: Row(
              children: [
                Visibility(
                  visible: widget.upgradeInfo.upgradeType==1,
                  child: Expanded(
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 10),
                      child: OutlinedButton(
                        child: Text(
                          '下次再说',
                          style: _textStyle,
                        ),
                        onPressed: () {

                          Navigator.of(context).pop();
                        },
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    child: OutlinedButton(
                      child: Text(
                        btnTxt,
                        style: _textStyle,
                      ),
                      onPressed: () {
                        if (_downloadProgress != 0&&_downloadProgress != 100) {
                          widget.onClickWhenDownload("正在更新中");
                          return;
                        }
                        widget.onClickWhenNotDownload();
//            Navigator.of(context).pop();
                      },
                    ),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
//size.substring(0,size.indexOf(".")+3) 小数点位数
  getPrintSize(limit){
    String  size = "";
    //内存转换
    if(limit < 1 * 1024){                            //小于0.1KB，则转化成B
      size = limit.toString();
      size = size.substring(0,size.indexOf(".")+3) + "  B";
    }else if(limit < 1 * 1024 * 1024){            //小于0.1MB，则转化成KB
      size = (limit/1024).toString();
      size = size.substring(0,size.indexOf(".")+3) + "  KB";
    }else if(limit < 1 * 1024 * 1024 * 1024){        //小于0.1GB，则转化成MB
      size = (limit/(1024 * 1024)).toString() ;
      print(size.indexOf("."));
      size = size.substring(0,size.indexOf(".")+3) + "  MB";
    }else{                                            //其他转化成GB
      size = (limit/(1024 * 1024 * 1024)).toString();
      size = size.substring(0,size.indexOf(".")+3) + "  GB";
    }
    return size;
  }
  set progress(_progress) {
    setState(() {
      _downloadProgress = _progress;
//      if (_downloadProgress == 100) {
//        Navigator.of(context).pop();
//        _downloadProgress = 0;
//      }
    });
  }
}